package com.seafood.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import java.math.BigDecimal;

/**
 * 订单详情实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "order_items")
public class OrderItem extends BaseEntity {
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id", nullable = false)
    private Order order;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", nullable = false)
    private Product product;
    
    @Size(max = 200, message = "产品名称长度不能超过200字符")
    @Column(name = "product_name", nullable = false, length = 200)
    private String productName;
    
    @NotNull(message = "数量不能为空")
    @Positive(message = "数量必须大于0")
    @Column(nullable = false)
    private Integer quantity;
    
    @NotNull(message = "单价不能为空")
    @DecimalMin(value = "0.0", message = "单价不能小于0")
    @Column(name = "unit_price", nullable = false, precision = 10, scale = 2)
    private BigDecimal unitPrice;
    
    @NotNull(message = "小计不能为空")
    @DecimalMin(value = "0.0", message = "小计不能小于0")
    @Column(name = "total_price", nullable = false, precision = 10, scale = 2)
    private BigDecimal totalPrice;
    
    @Column(name = "batch_number", length = 100)
    private String batchNumber;
    
    @Lob
    private String specifications;
    
    /**
     * 计算小计金额
     */
    public void calculateTotalPrice() {
        if (quantity != null && unitPrice != null) {
            this.totalPrice = unitPrice.multiply(BigDecimal.valueOf(quantity));
        }
    }
}